<template>
  <div class="user">
    <h2>修改密码</h2>
    <el-form 
      ref="passForm" 
      :model="passForm" 
      :rules="passRules"
      label-width="100px"
    >
      <el-form-item label="原密码 :" prop="oldPass">
        <el-input type="password" v-model="passForm.oldPass"></el-input>
      </el-form-item>
      <el-form-item label="新密码 :" prop="pass">
        <el-input type="password" v-model="passForm.pass"></el-input>
      </el-form-item>
       <el-form-item label="确认密码 :" prop="pass2">
        <el-input type="password" v-model="passForm.pass2"></el-input>
      </el-form-item>
      <el-button type="primary" @click="altPass">修改密码</el-button>
    </el-form>
  </div>
</template>
 
<script>
import {mapState,mapMutations} from "vuex"
export default {
  name: 'AltPass',
  data(){
    return{
      passForm:{
        oldPass:"",
        pass:"",
        pass2:""
      },
      passRules:{
        oldPass:[
          { required: true, trigger: 'change' },
        ],
        pass:[
          { required: true, trigger: 'change' },
        ],
        pass2:[
          { required: true, trigger: 'change' },
          {
            validator:(rule,value,cb)=>{
              // 检测两次密码是否相同
              if(value !== this.passForm.pass){
                cb(new Error("两次密码输入不一致"))
              }
              cb()
            }
          }
        ]
      }
    }
  },
  methods:{
    ...mapMutations(["updateAdminInfo","exitAdminInfo"]),
    altPass(){
      this.$refs.passForm.validate(async v=>{
        if(!v) return false
        let {data} = await this.$axios.post("/admin/adminInfo/altPass",{
          oldPass:this.passForm.oldPass,
          pass:this.passForm.pass
        })
        if(data.code) return this.$message.warning(data.mes)
        this.$message.success(data.mes)
        // 退出登录
        this.exitAdminInfo()
        window.location.reload()
      })
    }
  },
  computed:{
    ...mapState(['adminInfo'])
  }
}
</script>

<style scoped lang='less'>
.user{
    margin-bottom: 45px;
    h2{
      width: 100%;
      height: 50px;
      line-height: 40px;
      font-size: 20px;
      color: purple;
      box-sizing: border-box;
      border-bottom: 1px solid #eee;
    }
    #name{
      background-color: rgb(230, 229, 229);
      color: red;
      padding: 5px 10px;
    }
}
</style>